package com.dt.app.controller.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dt.app.api.shopping.service.IDsCategoryService;
import com.dt.app.api.shopping.service.IDsProductService;
import com.dt.app.api.shopping.service.IDsPromotionService;
import com.dt.app.api.test.service.IHyUserService;
import com.dt.app.common.response.ResultUtil;
import com.dt.app.controller.base.BaseController;
import com.dt.app.modules.IndexBean;
import com.dt.app.modules.shopping.entities.DsCategory;
import com.dt.app.modules.shopping.entities.DsProduct;
import com.dt.app.modules.shopping.entities.DsPromotion;
import com.dt.app.modules.shopping.vo.DsCategoryBean;
import com.dt.app.modules.shopping.vo.DsProductBean;
import com.dt.app.modules.test.entities.HyUser;
import com.dt.app.modules.test.vo.HyUserBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/ws/v1")
public class IndexTestController  extends BaseController {
    @Autowired
    private IHyUserService hyUserService;
    @Autowired
    private IDsPromotionService iDsPromotionService;
    @Autowired
    private IDsProductService iDsProductService;
    @Autowired
    private IDsCategoryService iDsCategoryService;
    @GetMapping("index/{id}")
    @ApiOperation(value = "id查询会员信息")
    public ResultUtil<IndexBean> getHyById(@PathVariable String id) {
        try {
            IndexBean indexBean = new IndexBean();

            HyUser entity = hyUserService.getById(id);//null
            HyUserBean hyUser = hyUserService.getBean(entity);
            indexBean.setHyUser(hyUser);

//            "栏目信息"前50条
            QueryWrapper<DsCategory> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("ds_category_id");
            wrapper.last("limit 50");
            /*Page<DsCategory> page = new Page<>(1,50);
            IPage iPage = iDsCategoryService.page(page,wrapper);
            iPage.getCurrent();//页码
            iPage.getSize();//每页数量
            iPage.getTotal();//总数量
            iPage.getRecords();// 数据列表
            List<DsCategory> categoryList1 = iPage.getRecords();*/

            List<DsCategory> categoryList = iDsCategoryService.list(wrapper);
            List<DsCategoryBean> categoryBean = new ArrayList<>();
            if (categoryList!=null) {
                categoryList.forEach(it -> {
                    DsCategoryBean bean = iDsCategoryService.getBean(it);
                    categoryBean.add(bean);
                });
            }

//            "推广信息" 前20条 buy_seq desc , seq desc
            QueryWrapper<DsProduct> productWrapper = new QueryWrapper<>();
            productWrapper.orderByDesc("buy_seq").orderByDesc("seq");
            productWrapper.last("limit 20");

            List<DsProduct> productList = iDsProductService.list(productWrapper);// null
//            List<DsProduct> productList = null;
//            List<DsProduct> productList = new ArrayList<>();
            List<DsProductBean> indexProduct = new ArrayList<>();
            if (productList !=null && !productList.isEmpty()) {
                productList.forEach(it -> {
                    DsProductBean bean = iDsProductService.getBean(it);
                    indexProduct.add(bean);
                });
            }
            indexBean.setIndexProduct(indexProduct);

            return ResultUtil.success(indexBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("系统错误请联系管理员！");
        }
    }
}